/**
 * The MarkerClusterer object.
 * 
 * @type {MarkerCluster}
 */
var mc = null;

/**
 * The Map object.
 * 
 * @type {google.maps.Map}
 */
var map = null;

/**
 * The MarkerManager object.
 * 
 * @type {MarkerManager}
 */
var mgr = null;

/**
 * The KmlLayer object.
 * 
 * @type {google.maps.KmlLayer}
 */
var kmlLayer = null;

/**
 * The FusionTablesLayer object.
 * 
 * @type {google.maps.FusionTablesLayer}
 */
var fusionLayer = null;

/**
 * KML layer display/hide flag.
 * 
 * @type {boolean}
 */
var showKmlLayer = false;

/**
 * Fusion Tables layer display/hide flag.
 * 
 * @type {boolean}
 */
var showFusionLayer = false;

/**
 * Fusion Tables layer heatmap flag.
 * 
 * @type {boolean}
 */
var showFusionLayerHeatmap = false;


/**
 * Marker Clusterer display/hide flag.
 * 
 * @type {boolean}
 */
var showMarketClusterer = false;

/**
 * Marker Manager display/hide flag.
 * 
 * @type {boolean}
 */
var showMarketManager = false;

/**
 * InfoWindow zum Anzeigen der EventDetails
 * 
 * @type {google.maps.InfoWindow}
 */
var infoWindow = new google.maps.InfoWindow();

/**
 * Initialisiert die Karte und setzt die Marker
 */
function initialize() {
	map = new google.maps.Map(document.getElementById('map'), {
		zoom : 5,
		center : new google.maps.LatLng(49.0047493,8.3411503),
		mapTypeId : google.maps.MapTypeId.ROADMAP
	});
	toggleMarkerManager();

}

/**
 * Toggles heatmap use on Fusion Tables layer.
 */
function toggleFusionHeatmap() {
	if (fusionLayer) {
		showFusionLayerHeatmap = !showFusionLayerHeatmap;
		fusionLayer.set('heatmap', showFusionLayerHeatmap);
	}
}

/**
 * Toggles Fusion Tables layer visibility.
 */
function toggleFusion() {
	if (!fusionLayer) {
		fusionLayer = new google.maps.FusionTablesLayer(232506, {
			suppressInfoWindows : false
		});
		showFusionLayerHeatmap = false;
		fusionLayer.set('heatmap', showFusionLayerHeatmap);
	}
	showFusionLayer = !showFusionLayer;
	var li = document.getElementById('fusion-hm-li');
	if (showFusionLayer) {
		fusionLayer.setMap(map);
		li.style.visibility = 'visible';
	} else {
		fusionLayer.setMap(null);
		li.style.visibility = 'hidden';
	}
}

/**
 * Toggles KML layer visibility.
 */
function toggleKmlLayer() {
	if (!kmlLayer) {
		var kmlUrl = window.location.href.substring(0, 1 + window.location.href
				.lastIndexOf('/'))
				+ 'markers.kml';
		kmlLayer = new google.maps.KmlLayer(kmlUrl, {
			preserveViewport : true,
			suppressInfoWindows : false
		});
	}
	showKmlLayer = !showKmlLayer;
	if (showKmlLayer) {
		kmlLayer.setMap(map);
	} else {
		kmlLayer.setMap(null);
	}
}

/**
 * Toggles Marker Clusterer visibility.
 */
function toggleMarkerClusterer() {
	showMarketClusterer = !showMarketClusterer;
	if (showMarketClusterer) {
		if (mc) {
			mc.addMarkers(markers.locations);
		} else {
			mc = new MarkerClusterer(map, markers.locations, {
				maxZoom : 19
			});
		}
	} else {
		mc.clearMarkers();
	}
}



/**
 * Toggles Marker Manager visibility.
 */
function toggleMarkerManager() {
	showMarketManager = !showMarketManager;
	if (mgr) {
		if (showMarketManager) {
			mgr.addMarkers(markers.events, 0, 15);

			mgr.refresh();
		} else {
			mgr.clearMarkers();
			mgr.refresh();
		}
	} else {
		mgr = new MarkerManager(map, {
			trackMarkers : true,
			maxZoom : 15
		});
		google.maps.event.addListener(mgr, 'loaded', function() {
			mgr.addMarkers(markers.events, 0, 15);
			// mgr.addMarkers(markers.places, 6, 11);
			// mgr.addMarkers(markers.locations, 12);
			mgr.refresh();
		});
	}
}



// Kreiert für jedes Event einen Marker und verknüpft ihn per Listener mit dem InfoWindow
if (markers) {

	for ( var i = 0; i < markers.events.length; i++) {
		var eventDetail = markers.events[i];
		markers.events[i] = new google.maps.Marker({
			title : eventDetail.name,
			position : new google.maps.LatLng(eventDetail.location[0],
					eventDetail.location[1]),
			clickable : true,
			draggable : true,
			flat : true
		});
		var marker = markers.events[i];
						
		(function(marker, infoWindow, eventDetail) {
			google.maps.event.addListener(marker, 'click', function() {
				infoWindow.setContent(eventDetail.content);
				infoWindow.open(map, marker);
			});
		})(marker, infoWindow, eventDetail);
	}
}

google.maps.event.addDomListener(window, 'load', initialize);
